package com.ysg.widget.edittext;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.ysg.R;
import com.ysg.enums.EditBgEnum;
import com.ysg.enums.InputTypeEnum;
import com.ysg.utils.YStringUtil;

/**
 * 通用输入框
 */
public class YEditText extends LinearLayout {

    public interface OnEditTextListener{
        void onClearListener();
        void onTextChangedListener(String s);
    }

    private OnEditTextListener listener;
    public void setOnEditListener(OnEditTextListener listener){
        this.listener = listener;
    }

    private View parent;
    private EditText editText;
    private ImageView ivPassword, ivClear;
    private boolean showClear, showPassword = false, showEmoji;
    private int inputType, editBg;
    private String hint;

    public YEditText(Context context) {
        super(context);
    }

    public YEditText(Context context, AttributeSet attrs) {
        super(context, attrs);

        initView(context);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.YEditText);
        showClear = array.getBoolean(R.styleable.YEditText_clear, true);
        showEmoji = array.getBoolean(R.styleable.YEditText_emoji, false);
        hint = array.getString(R.styleable.YEditText_hint);
        inputType = array.getInt(R.styleable.YEditText_inputType, -1);
        editBg = array.getInt(R.styleable.YEditText_editBg, -1);
        array.recycle();

        setUI();
        initListener();
    }

    private void initView(Context context) {
        View view = View.inflate(context, R.layout.widget_edit_text, this);
        parent = view.findViewById(R.id.parent);
        editText = view.findViewById(R.id.editText);
        ivPassword = view.findViewById(R.id.ivPassword);
        ivClear = view.findViewById(R.id.ivClear);
    }

    private void setUI(){
        //输入框类型：密码；手机；金额
        InputTypeEnum inputTypeEnum = InputTypeEnum.getByType(inputType);
        if (inputTypeEnum != null){
            switch (inputTypeEnum){
                case password:
                    editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    ivPassword.setVisibility(VISIBLE);
                    break;
                case phone:
                    editText.setInputType(InputType.TYPE_CLASS_PHONE);
                    break;
                case money:
                    editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                    break;
            }
        }
        //提示语
        if (YStringUtil.isNotEmpty(hint)){
            editText.setHint(hint);
        }
        //背景框
        EditBgEnum editBgEnum = EditBgEnum.getByType(editBg);
        if (editBgEnum != null){
            switch (editBgEnum){
                case no:
                    parent.setBackground(null);
                    break;
                case radius:
                    parent.setBackgroundResource(R.drawable.shape_edit_radius_stroke);
                    break;
                case circle:
                    parent.setBackgroundResource(R.drawable.shape_edit_oval_stroke);
                    break;
            }
        }
    }

    private void initListener() {
        //处理清空按钮：是否显示隐藏
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (showClear){
                    if (YStringUtil.isNotEmpty(s.toString())){
                        ivClear.setVisibility(VISIBLE);
                    }else{
                        ivClear.setVisibility(GONE);
                    }
                }
                if (listener != null){
                    listener.onTextChangedListener(s.toString());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

        //清空
        ivClear.setOnClickListener((v)->{
            editText.setText("");
            if (listener != null){
                listener.onClearListener();
            }
        });
        //密码
        ivPassword.setOnClickListener((v)->{
            togglePassword();
        });
        //表情包
        if (!showEmoji) {
            InputFilter[] oldFilters = editText.getFilters();
            InputFilter[] newFilters = new InputFilter[oldFilters.length + 1];
            newFilters[oldFilters.length] = new EmojiExcludeFilter();
            System.arraycopy(oldFilters, 0, newFilters, 0, oldFilters.length);
            editText.setFilters(newFilters);
        }
    }

    /**
     * 切换显示密码
     */
    private void togglePassword() {
        showPassword = !showPassword;
        final int selectionEnd = editText.getSelectionEnd();
        final int selectionStart = editText.getSelectionStart();
        editText.setSelection(selectionStart, selectionEnd);
        editText.setInputType(showPassword ? InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD : InputType.TYPE_TEXT_VARIATION_PASSWORD);
        editText.setInputType(
                InputType.TYPE_CLASS_TEXT | (showPassword ?
                                InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD :
                                InputType.TYPE_TEXT_VARIATION_PASSWORD));
        ivPassword.setImageResource(showPassword? R.drawable.svg_ic_show_password_24dp: R.drawable.svg_ic_hide_password_24dp);
    }

    private static class EmojiExcludeFilter implements InputFilter {

        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            for (int i = start; i < end; i++) {
                int type = Character.getType(source.charAt(i));
                if (type == Character.SURROGATE || type == Character.OTHER_SYMBOL) {
                    return "";
                }
            }
            return null;
        }
    }




}
